//======================================================================
//    This file is part of baijie_interface
//
//    Copyright (C) 2023 Shenzhen Baijie Technology Co., Ltd.All rights reserved.
//
//    filename : sim_interface.cpp
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//======================================================================

#include "sim_interface.h"

sim_interface::sim_interface(QObject *parent) : QObject(parent)
{

}
sim_interface::~sim_interface()
{

}

QString sim_interface::executeLinuxCmd(QString strCmd)
{
    QProcess pro;
    pro.start("bash", QStringList() <<"-c" << strCmd);
    pro.waitForFinished(-1);
    QString strResult = pro.readAllStandardOutput();
    pro.close();
    return strResult;
}

QString sim_interface::simDisconnect()
{
    QString strCmd = QString("nmcli connection down ppp0");
    QString strResult = executeLinuxCmd(strCmd);
    bool DisconnectResult=strResult.contains("successfully deactivated",Qt::CaseInsensitive);

    if(DisconnectResult == true)
    {
        strResult = "Disconnect successful!";
        return QString(1);
    }
    else
    {
        strResult = "Disconnect failed!";
        return 0;
    }
    return 0;
}

void sim_interface::setSimEnable(int flag)
{
    if(flag == 1)
    {
        QString strCmd = QString("nmcli connection up ppp0");
        executeLinuxCmd(strCmd);
    }
    else if(flag == 0)
    {
        QString strCmd = QString("nmcli connection down ppp0");
        executeLinuxCmd(strCmd);
    }
}

QString sim_interface::simConnectAgain()
{
    QString strCmd;
    QString strResult;

    strCmd = QString(". /usr/helperboard/qt/bin/get_4g_gpio.sh");
    strResult = executeLinuxCmd(strCmd);
    strResult.remove(strResult.size()-1,1);
    QString portName = strResult.mid(0,1);
    QString portNum = strResult.mid(1,strResult.size());

    strCmd = QString("gpio-test.64 w %1 %2 0").arg(portName).arg(portNum);
    executeLinuxCmd(strCmd);
    strCmd = QString("gpio-test.64 w %1 %2 1").arg(portName).arg(portNum);
    executeLinuxCmd(strCmd);

    QFileInfo fileInfo("/etc/NetworkManager/system-connections/ppp0");
    if(fileInfo.isFile() == true)
    {
        strCmd = QString("nmcli connection delete ppp0");
        strResult = executeLinuxCmd(strCmd);
    }

    strCmd = QString("nmcli connection add con-name ppp0 ifname ttyUSB2 autoconnect yes type gsm apn 3gnet user uninet password uninet");
    strResult = executeLinuxCmd(strCmd);
    bool connectResult = strResult.contains("successfully added",Qt::CaseInsensitive);

    if(connectResult == 1)
    {
        sleep(45);
        strResult = executeLinuxCmd("ifconfig |grep ppp0 |wc -l");
        if(strResult == "1\n")
        {
            return QString(1);
        }
        else
        {
            return 0;
        }
    }
    else
    {
        strResult = "Connection failed!";
        strCmd = QString("nmcli connection delete ppp0");
        strResult = executeLinuxCmd(strCmd);

        bool deleteResult=strResult.contains("successfully deleted",Qt::CaseInsensitive);
        if(deleteResult == 1)
        {
            strResult = "Delete successful!";
        }
        else
        {
            strResult = "Delete failed!";
        }

        return 0;
    }
    return 0;

}

QString sim_interface::simConnect()
{
    QString strCmd;
    QString strResult;
    strCmd = QString(". /usr/helperboard/qt/bin/get_4g_gpio.sh");
    strResult = executeLinuxCmd(strCmd);
    strResult.remove(strResult.size()-1,1);
    QString strPortName = strResult.mid(0,1);
    int num = strResult.mid(1,strResult.size()).toInt();

    QChar *tmp = new QChar[7];
    tmp = strPortName.data();
    char charPortName = tmp->toLatin1();

    strCmd = QString("nmcli con show --active |grep ppp0 |wc -l");
    strResult = executeLinuxCmd(strCmd);
    if(strResult == "1\n")
    {
        return QString(1);
    }

    char *state = (char *)"out";
    int portNum = calcPortNum(charPortName,num);

    bool isExist = getFileName(portNum);
    if(isExist == false)
    {
        if(gpioExport(portNum) == 0)
        {
            if(setGpioState(portNum, state) == 0)
            {
                if(setGpioValue(portNum, 1) == 0)
                {
                    sleep(5);
                }
            }
        }
        else
        {
            gpioUnexport(portNum);
            return 0;
        }
    }

    QFileInfo fileInfo("/etc/NetworkManager/system-connections/ppp0");
    if(fileInfo.isFile() == true)
    {
        strCmd = QString("nmcli connection delete ppp0");
        strResult = executeLinuxCmd(strCmd);
    }

    strCmd = QString("nmcli connection add con-name ppp0 ifname ttyUSB2 autoconnect yes type gsm apn 3gnet user uninet password uninet");
    strResult = executeLinuxCmd(strCmd);
    bool connectResult = strResult.contains("successfully added",Qt::CaseInsensitive);

    if(connectResult == 1)
    {
        sleep(1);
        strResult = executeLinuxCmd("ifconfig |grep ppp0 |wc -l");
        if(strResult == "1\n")
        {
            return QString(1);
        }
        else
        {
            return 0;
        }
    }
    else
    {
        strResult = "Connection failed!";
        strCmd = QString("nmcli connection delete ppp0");
        strResult = executeLinuxCmd(strCmd);

        bool deleteResult=strResult.contains("successfully deleted",Qt::CaseInsensitive);
        if(deleteResult == 1)
        {
            strResult = "Delete successful!";
        }
        else
        {
            strResult = "Delete failed!";
        }

        return 0;
    }
    return 0;
}

QString sim_interface::getSimStatus()
{
    QString strCmd;
    QString strResult;
    QString statusResult;

    strCmd = QString("nmcli con show --active |grep ppp0 |wc -l");
    strResult = executeLinuxCmd(strCmd);
    if(strResult == "0\n")
    {
        return 0;
    }

    strCmd = QString("nmcli con show ppp0 |grep NAME");strCmd = " "+strCmd;
    statusResult.append(executeLinuxCmd(strCmd).simplified()+"\n");
    strCmd = QString("nmcli con show ppp0 |grep STATE");strCmd = " "+strCmd;
    statusResult.append(executeLinuxCmd(strCmd).simplified()+"\n");
    strCmd = QString("nmcli con show ppp0 |grep VPN");strCmd = " "+strCmd;
    statusResult.append(executeLinuxCmd(strCmd).simplified()+"\n");strCmd = " "+strCmd;
    strCmd = QString("nmcli con show ppp0 |grep autoconnect | sed -n 1p");
    statusResult.append(executeLinuxCmd(strCmd).simplified()+"\n");strCmd = " "+strCmd;
    strCmd = QString("nmcli con show ppp0 |grep ADDRESS |sed -n 1p");
    statusResult.append(executeLinuxCmd(strCmd).simplified()+"\n");strCmd = " "+strCmd;

    return statusResult;
}

QString sim_interface::getSimModuleStatus()
{
    QString strCmd;
    QString strResult;
    QString statusResult;

    strCmd = QString("mmcli --list-modems");    // Check whether there are modules
    strResult = executeLinuxCmd(strCmd);
    QString str = strResult.section("/",5,5);str = str.section(" ",0,0);
    if(str.isEmpty())
    {
        return "";
    }
    strCmd = QString("mmcli -m %1 |grep revision").arg(str);
    strCmd = executeLinuxCmd(strCmd);
    strCmd = strCmd.section(":",1,1);
    statusResult.append(strCmd)+"\n";

    strCmd = QString("mmcli -m %1 |grep supported").arg(str);
    strCmd = executeLinuxCmd(strCmd);
    strCmd = strCmd.section("\n",0,0);strCmd = strCmd.section(":",1,1);
    strCmd = strCmd+"\n";
    statusResult.append(strCmd);

    strCmd = QString("mmcli -m %1 |grep equipment").arg(str);
    strCmd = executeLinuxCmd(strCmd);
    strCmd = strCmd.section(":",1,1);
    statusResult.append(strCmd)+"\n";

    strCmd = QString("mmcli -m %1 |grep ports").arg(str);
    strCmd = executeLinuxCmd(strCmd);
    strCmd = strCmd.section(":",1,1);
    statusResult.append(strCmd)+"\n";

    strCmd = QString("mmcli -m %1 |grep state").arg(str);
    strCmd = executeLinuxCmd(strCmd);
    if(strCmd.contains("failed",Qt::CaseInsensitive))
    {
        strCmd = strCmd.section("\n",0,0);strCmd = strCmd.section("m",1,1);
        strCmd = strCmd.remove(6,strCmd.size()-1);
        strCmd = " "+strCmd+"\n";
    }
    else
    {
        QString str1 = QString("nmcli con show --active |grep ppp0 |wc -l");
        str1 = executeLinuxCmd(str1);
        if(str1 == "0\n")
        {
            strCmd = strCmd.section("\n",0,0);strCmd = strCmd.section(":",1,1);strCmd = strCmd+"\n";
        }
        else
        {
            strCmd = strCmd.section("\n",0,0);strCmd = strCmd.section("m",1,1);strCmd.remove(9,strCmd.size()-1);
            strCmd = " "+strCmd+"\n";
        }
    }
    statusResult.append(strCmd);

    strCmd = QString("mmcli -m %1 |grep power").arg(str);
    strCmd = executeLinuxCmd(strCmd);
    strCmd = strCmd.section(":",1,1);
    statusResult.append(strCmd)+"\n";

    strCmd = QString("mmcli -m %1 |grep access").arg(str);
    strCmd = executeLinuxCmd(strCmd);
    strCmd = strCmd.section(":",1,1);
    if(strCmd.isEmpty())
    {
        strCmd = QString(" %1 \n").arg("no");
        statusResult.append(strCmd);
    }
    else
    {
        statusResult.append(strCmd)+"\n";
    }
    strCmd = QString("mmcli -m %1 |grep signal").arg(str);
    strCmd = executeLinuxCmd(strCmd);
    strCmd = strCmd.section(":",1,1);
    statusResult.append(strCmd)+"\n";

    strCmd = QString("mmcli -m %1 |grep IP").arg(str);
    strCmd = executeLinuxCmd(strCmd);
    strCmd = strCmd.section(":",1,1);
    statusResult.append(strCmd)+"\n";

    strCmd = QString("mmcli -m %1 |grep name").arg(str);
    strCmd = executeLinuxCmd(strCmd);
    strCmd = strCmd.section(":",1,1);
    if(strCmd.isEmpty())
    {
        strCmd = QString(" %1 \n").arg("no");
        statusResult.append(strCmd);
    }
    else
    {
        statusResult.append(strCmd)+"\n";
    }
    return statusResult;
}

